 
import  AopFactory  from  '../AopFactory';
import JSZip from  'jszip'
import  ConfigLoad  from  './ConfigLoad';
import  LibDirLoad from './LibDirLoad'

/**
 * 扫描路径
 * @param path
 * @param name
 * @returns {*}
 */
export default class JsrLoad{
    constructor(){
        this. configs=[]
        this.libDirs=[];
        this.runDirs=[];
    }
    load(moduleName){

        this. configs=new ConfigLoad().load(moduleName);
         this. libDirs=new LibDirLoad().load(moduleName);

        let dirs=new Dirs();
        for(let i=0; i<this.configs.length;i++){
          let disss=  dirs.isDir(this.configs[i],this.libDirs)
            this.runDirs.push(...disss)
        }
    }

    _load(){
        let aopFactory=new AopFactory();
        for (let i=0;i<this.runDirs.length;i++){
            let jsr= qguan.findData(moduleName,"","","jsrLoad")
            jsr.then(function (data) {
                JSZip.loadAsync(data).then(function (zip) {
                  let dirNames=  this.runDirs[i].dirNames;
                    for(let l=0;l<dirNames.length;l++){
                        zip.file(this.runDirs[i].diNames[l]).async("string")
                            .then(function (code) {
                                let filename = diNames[l].substring(diNames[l].lastIndexOf('\/') + 1, diNames[l].lastIndexOf('.'));
                                let obj =aopFactory.createCodeObj(moduleName,filename,code);
                            });
                    }
                });
            })
        }

    }
}

/**
 * 通过config  需要加载的路径
 *
 */
class Dirs{
    constructor(){
        this. dirs=[]
        this.dirNames={}
    }
    isDir(config,libDirs){
        this.dirs=[]
        this.dirNames={}
        for(let l=0;l<libDirs.length;l++){
            if(libDirs[l][1].indexOf(config) != -1 ){
                let j=dirNames[libDirs[l][0]]
                if(j){
                    this. dirs.push(new Dir(libDirs[l][0],libDirs[l][1]))
                }else {
                    this. dirs[j].dirs.push(dir)
                }
            }
        }
        return this.dirs;
    }

}

class Dir{
    constructor(module,dir) {
        this.module = module
        this.dirs=[dir]
    }

}


